<%
var headerPart = {
%>
<% include("/include/treetable.html"){} %>
<script type="text/javascript">
    $(document).ready(function() {
        var tpl = $("#treeTableTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
        var data = ${@JsonMapper.toJsonString(list)}, ids = [], rootIds = [];
        for (var i=0; i<data.length; i++){
            ids.push(data[i].id);
        }
        ids = ',' + ids.join(',') + ',';
        for (var i=0; i<data.length; i++){
            if (ids.indexOf(','+data[i].parentId+',') == -1){
                if ((','+rootIds.join(',')+',').indexOf(','+data[i].parentId+',') == -1){
                    rootIds.push(data[i].parentId);
                }
            }
        }
        for (var i=0; i<rootIds.length; i++){
            addRow("#treeTableList", tpl, data, rootIds[i], true);
        }
        $("#treeTable").treeTable({expandLevel : 5});
    });
    function addRow(list, tpl, data, pid, root){
        for (var i=0; i<data.length; i++){
            var row = data[i];
            if ((${@StringUtils.jsGetVal('row.parentId')}) == pid){
                $(list).append(Mustache.render(tpl, {
                    dict: {
                        oauth: getDictLabel(${@JsonMapper.toJsonString(@DictUtils.getDictList('yes_no'))}, row.oauth),
                        blank123:0}, pid: (root?0:pid), row: row
                }));
                addRow(list, tpl, data, row.id);
            }
        }
    }
    function createMenu(){
        layer.confirm('确定替换微信现有的菜单？', {icon: 3, title: '提示信息'}, function (index) {
            var groupId = "1";
            $.ajax({
                url:"${ctx}/wx/wxAccountMenu/createMenu/"+groupId,
                type: 'POST',
                success: function(result) {
                    if(result=="success"){
                        layer.msg("生成微信菜单成功");
                    }
                    layer.close(index);
                },
                error:function(){
                    layer.msg("生成微信菜单成功");
                    layer.close(index);
                }
            });
        });
    }
</script>
<% }; %>
<% layout("/layouts/micro.html",{headerSection:headerPart}){ %>
<div class="layui-tab layui-tab-brief childrenBody">
	<ul class="layui-tab-title">
		<li class="layui-this"><a href="${ctx}/wx/wxAccountMenu/">公众号菜单列表</a></li>
		<%if(shiro.hasPermission("wx:wxAccountMenu:edit")){%>
			<li><a href="${ctx}/wx/wxAccountMenu/form">公众号菜单添加</a></li>
		<%}%>
	</ul>
	<div class="layui-tab-content">
	<blockquote class="layui-elem-quote quoteBox" >
		<form  action="${ctx}/wx/wxAccountMenu/" method="post" class="layui-form">
			<div class="layui-inline">
				<label>菜单名称:</label>
				<div class="layui-input-inline">
					<input type="text" name="name" class="layui-input"/>
				</div>
				<button class="layui-btn search_btn"  lay-filter="*" lay-submit>搜索</button>
			</div>
			<div class="layui-inline">
				<a class="layui-btn layui-btn-normal " onclick="createMenu()">发布菜单</a>
			</div>
		</form>
	</blockquote>
	<#message content="${message!}"/>
	<table id="treeTable" class="layui-table table-condensed" >
		<thead>
			<tr>
				<th>菜单名称</th>
				<th>菜单键值</th>
				<th>事件名称</th>
				<th>是否需授权</th>
				<th>更新时间</th>
				<%if(shiro.hasPermission("wx:wxAccountMenu:edit")){%><th>操作</th><%}%>
			</tr>
		</thead>
		<tbody id="treeTableList"></tbody>
	</table>
	<script type="text/template" id="treeTableTpl">
		<tr id="{{row.id}}" pId="{{pid}}">
			<td><a href="${ctx}/wx/wxAccountMenu/form?id={{row.id}}">
				{{row.name}}
			</a></td>
			<td >
				{{row.eventKey}}
			</td>
			<td>
				{{row.type}}
			</td>
			<td>
				{{dict.oauth}}
			</td>
			<td>
				{{row.updateDate}}
			</td>
			<%if(shiro.hasPermission("wx:wxAccountMenu:edit")){%>
			<td>
   				<a class="layui-btn layui-btn-xs layui-btn" href="${ctx}/wx/wxAccountMenu/form?id={{row.id}}">修改</a>
				<a class="layui-btn layui-btn-xs layui-btn-danger" href="${ctx}/wx/wxAccountMenu/delete?id={{row.id}}" onclick="return confirmx('确认要删除该公众号菜单及所有子公众号菜单吗？', this.href)">删除</a>
				<a class="layui-btn layui-btn-xs layui-btn-primary" href="${ctx}/wx/wxAccountMenu/form?parent.id={{row.id}}">添加下级公众号菜单</a>
			</td>
			<%}%>
		</tr>
	</script>
	</div>
</div>
<%}%>